﻿using Mrc.Config.Messages;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Mrc.Config
{
    public interface ISerializer
    {
 
        Task<string> SerializeAsync(ConfigMessages message);
        
        Task<ConfigMessages> DeserializeAsync(string body);
    }

    public class MessagesSerializer : ISerializer
    {
        public Task<string> SerializeAsync(ConfigMessages message)
        {
            if(message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            var json = JsonConvert.SerializeObject(message);
            return Task.FromResult(json);
        }
        public Task<ConfigMessages> DeserializeAsync(string body)
        {
            if(body == null)
            {
                return Task.FromResult(new ConfigMessages());
            }
            return Task.FromResult(JsonConvert.DeserializeObject<ConfigMessages>(body));
        }
    }
}
